﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Serviio.API;

namespace Serviio.Test
{
    public partial class FormAPITest : Form
    {
        ServiioConnection conn = null;

        public FormAPITest()
        {
            InitializeComponent();
        }

        private void FormAPITest_Load(object sender, EventArgs e)
        {
            listBoxActions.Items.Add("Metadata GET");
            listBoxActions.Items.Add("Metadata Post");
            listBoxActions.Items.Add("Presentation GET");
            listBoxActions.Items.Add("Presentation Post");
            listBoxActions.Items.Add("Repository GET");
            listBoxActions.Items.Add("Repository POST");
            listBoxActions.Items.Add("Status GET");
            listBoxActions.Items.Add("Status POST");
            listBoxActions.Items.Add("Transcoding GET");
            listBoxActions.Items.Add("Transcoding POST");
            listBoxActions.Items.Add("Library Status GET");
            listBoxActions.Items.Add("Ping GET");
            listBoxActions.Items.Add("Reference GET");
            listBoxActions.Items.Add("Version GET");
            listBoxActions.Items.Add("ForceVideoFilesMetadataUpdate");
            listBoxActions.Items.Add("ForceLibraryRefresh");
            listBoxActions.Items.Add("StartUpnpServer");
            listBoxActions.Items.Add("StopUpnpServer");
            listBoxActions.Items.Add("ExitServiio");
            listBoxActions.Items.Add("AdvertiseService");
        }

        private void buttonTest_Click(object sender, EventArgs e)
        {
            switch (listBoxActions.SelectedItem.ToString())
            {
                case "Metadata GET":
                    conn.Metadata.Refresh();
                    propertyGridObject.SelectedObject = conn.Metadata;
                    break;
                case "Metadata POST":
                    conn.Metadata.Update();
                    break;
                case "Presentation GET":
                    conn.Presentation.Refresh();
                    propertyGridObject.SelectedObject = conn.Presentation;
                    break;
                case "Presentation POST":
                    conn.Presentation.Update();
                    break;
                case "Repository GET":
                    conn.Repository.Refresh();
                    propertyGridObject.SelectedObject = conn.Repository;
                    break;
                case "Repository POST":
                    conn.Repository.Update();
                    break;
                case "Status GET":
                    conn.Status.Refresh();
                    propertyGridObject.SelectedObject = conn.Status;
                    break;
                case "Status POST":
                    conn.Status.Update();
                    break;
                case "Transcoding GET":
                    conn.Transcoding.Refresh();
                    propertyGridObject.SelectedObject = conn.Transcoding;
                    break;
                case "Transcoding POST":
                    conn.Transcoding.Update();
                    break;
                case "Library Status GET":
                    propertyGridObject.SelectedObject = conn.GetLibraryStatus();
                    break;
                case "Ping GET":
                    propertyGridObject.SelectedObject = new KeyValuePair<string, string> ("Ping", conn.IsAvailable.ToString());;
                    break;
                case "Reference GET":
                    conn.Reference.Refresh();
                    propertyGridObject.SelectedObject = conn.Reference;
                    break;
                case "Version GET":
                    propertyGridObject.SelectedObject = new KeyValuePair<string, string> ("Version", conn.Version.ToString());
                    break;
                case "ForceVideoFilesMetadataUpdate":
                    conn.ForceVideoFilesMetadataUpdate();
                    break;
                case "ForceLibraryRefresh":
                    conn.ForceLibraryRefresh();
                    break;
                case "StartUpnpServer":
                    conn.StartUpnpServer();
                    break;
                case "StopUpnpServer":
                    conn.StopUpnpServer();
                    break;
                case "ExitServiio":
                    conn.ExitServiio();
                    break;
                case "AdvertiseService":
                    conn.AdvertiseService();
                    break;
            }
        }

        private void buttonConnect_Click(object sender, EventArgs e)
        {
            conn = new ServiioConnection(textBoxServer.Text, textBoxPort.Text);
            if (conn.IsAvailable)
            {
                buttonConnect.Enabled = false;
                buttonTest.Enabled = true;
            }
            else
            {
                MessageBox.Show("Unable to connect!!", "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
